//
//  ListViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ListViewController.h"
#import "ListDetailViewController.h"

#import "ListTableViewCell.h"
#import "ListModel.h"

#define ListCell @"listcell"
@interface ListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    
    self.dataArray = [NSMutableArray array];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self getDataHandle];
    [self.tableView registerClass:[ListTableViewCell class] forCellReuseIdentifier:ListCell];
}
- (void)getDataHandle
{
    NSString *urlstr = @"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    [manager GET:urlstr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        
        NSDictionary *firstDic = [responseObject firstObject];
        
        NSMutableArray *refs = [firstDic objectForKey:@"refs"];

        for (NSDictionary *dic in refs) {
            ListModel *model = [[ListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
       
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"%@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCell forIndexPath:indexPath];
    
    cell.cellModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    
    //加>
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    ListModel *model = self.dataArray[indexPath.row];
//    cell.pic sd_setImageWithURL:[NSURL URLWithString:model.]
    
    
    //    cell.textLabel.text = @"123";
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListDetailViewController *listVC = [[ListDetailViewController alloc] init];
    ListModel *model = self.dataArray[indexPath.row];
    listVC.songlist_id = [model.songlist_id stringValue];
    [self.navigationController pushViewController:listVC animated:YES];
}

@end
